package com.cssy.yd.logic.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.service.IBasicPlaceofstorageService;
import com.cssy.yd.basic.service.IBasicStorageService;
import com.cssy.yd.logic.entity.LogicPoDO;
import com.cssy.yd.logic.entity.LogicPoReturnDO;
import com.cssy.yd.logic.entity.LogicPoReturnItemDO;
import com.cssy.yd.logic.entity.LogicPoWareDO;
import com.cssy.yd.logic.entity.LogicPoWareItemDO;
import com.cssy.yd.logic.entity.LogicSoCancelDO;
import com.cssy.yd.logic.entity.LogicSoCancelItemDO;
import com.cssy.yd.logic.entity.LogicSoDO;
import com.cssy.yd.logic.entity.LogicSoItemDO;
import com.cssy.yd.logic.entity.LogicSoReturnDO;
import com.cssy.yd.logic.entity.LogicSoReturnItemDO;
import com.cssy.yd.logic.entity.LogicSoWareDO;
import com.cssy.yd.logic.entity.LogicSoWareItemDO;
import com.cssy.yd.logic.service.ILogicPoReturnService;
import com.cssy.yd.logic.service.ILogicPoService;
import com.cssy.yd.logic.service.ILogicPoWareService;
import com.cssy.yd.logic.service.ILogicSoCancelItemService;
import com.cssy.yd.logic.service.ILogicSoCancelService;
import com.cssy.yd.logic.service.ILogicSoItemService;
import com.cssy.yd.logic.service.ILogicSoReturnService;
import com.cssy.yd.logic.service.ILogicSoService;
import com.cssy.yd.logic.service.ILogicSoWareItemService;
import com.cssy.yd.logic.service.ILogicSoWareService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.AjaxUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Controller
@RequestMapping("/yd/logic/cancel")
public class CancelController extends BaseController<LogicSoCancelDO> {
	private static final String MANAGER = "/yd/logic/socancel/CancelManager";// 列表页面
	private static final String EIDT = "/yd/logic/socancel/CancelOperation";// 添加、修改、显示页面

	@Resource(name = "logicSoService")
	private ILogicSoService logicSoService;
	
	@Resource(name = "logicSoItemService")
	private ILogicSoItemService logicSoItemService;
	
	@Resource(name = "logicSoWareService")
	private ILogicSoWareService logicSoWareService;
	
	@Resource(name = "logicSoWareItemService")
	private ILogicSoWareItemService logicSoWareItemService;
	
	@Resource(name = "logicSoCancelService")
	private ILogicSoCancelService logicSoCancelService;
	
	@Resource(name = "logicSoCancelItemService")
	private ILogicSoCancelItemService logicSoCancelItemService;
	
	@Resource(name = "basicwhService")
	private IBasicStorageService basicwhService;

	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;

	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	

	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		List<String> strlist=new ArrayList<String>();
		strlist.add("3");//
		strlist.add("4");
		strlist.add("5");
		scs.addCondition("billstatus",strlist);
		SearchResult result = logicSoCancelService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);

	}
	
	@RequestMapping(value = "/applyitemlist")
	public void applyitemlist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		SearchResult result = new SearchResult();
		if (id != null && !"".equals(id)) {
			List<LogicSoItemDO> pw = logicSoItemService.findItemListBysoId(id);
			List<LogicSoCancelItemDO> itemlist=new ArrayList<LogicSoCancelItemDO>();
			if(pw.size()>0){
				for(int i=0;i<pw.size();i++){
					LogicSoCancelItemDO pr=new LogicSoCancelItemDO();
					//pr.setBrand(pw.get(i).getBrand());//TODO
					pr.setCode(pw.get(i).getProdNo());
					pr.setName(pw.get(i).getProdName());
					pr.setUnit(pw.get(i).getProdUnit());
					pr.setSpec(pw.get(i).getProdSpecDesc());
					//pr.setJquantity(pw.get(i).getpBQuantity());//TODO
					//pr.setQuantity(pw.get(i).getpQuantity());//TODO
					//pr.setYgxquantity(pw.get(i).getYgxquantity());//TODO
					itemlist.add(pr);
				}
			}
			result.setResults(itemlist);
			result.setRecordCount(itemlist.size());
		}
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
		SearchConditions scs1 = new SearchConditions();
		scs1.setRemovedProperty(null);// 判断是否有删除标识
//		SearchResult sr = basicBillingService.findListByExample(scs1);
//		List<BasicBillingDO> bb = sr.getResults();
//		model.addAttribute("bb", bb);
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		model.addAttribute("solist", findsolist());
		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			model.addAttribute("flag", "ADD");
		} else {
			LogicSoCancelDO lrd = logicSoCancelService.findListById(id);
			SearchConditions scs2 = new SearchConditions();
			scs2.addCondition("soid", lrd.getId(), SearchCondition.OP_EQUAL);
			scs2.setRemovedProperty(null);
			scs2.setLimitPage(false);
			scs2.setOrderBy(null);
			List<LogicSoCancelItemDO> lpitem = logicSoCancelItemService.findListByExample(scs2).getResults();
			model.addAttribute("soreturn", lrd);
			model.addAttribute("returnitem", lpitem);
			model.addAttribute("flag", "EDIT");
		}
		return EIDT;
	}


	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicSoCancelDO curAP, BindingResult result, Model model)
			throws SyBaseException {
		BodyMsg bodyMap = BodyMsg.getInstance();
		String itemdata = request.getParameter("itemdata");
		if (itemdata == null && "".equals(itemdata)) {
			bodyMap.isFail();
			bodyMap.configWebInfor("物料不能为空！");
			return bodyMap.modelMap;
		}
		try {
			logicSoCancelService.xsSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
			bodyMap.configWebId(curAP.getId());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;

	}
	
	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				LogicSoCancelDO ld=logicSoCancelService.findListById(field);
				if(ld!=null){
					ld.setRemoved(1);
					logicSoCancelService.update(ld, SessionUtil.getUser(request));
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}

	/**
	 * 获取全部未出库的销售单号
	 * @return
	 */
	private List<LogicSoDO> findsolist(){
		/*//原程序
		SearchConditions scs = new SearchConditions();
		scs.addCondition("billstatus","2",SearchCondition.OP_EQUAL);
		SearchResult result= logicSoService.findListByExample(scs);
		*/
		/*修改，状态6也可销售改单*/
		SearchConditions scs = new SearchConditions();
		List<String> strlist=new ArrayList<String>();
		strlist.add("2");//
		strlist.add("6");
		scs.addCondition("billstatus",strlist);
		SearchResult result= logicSoService.findListByExample(scs);
		
		return result.getResults();
		
	}
	
}
